package com.nsu.evaluation.service.impl;

import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.dao.FeedbackReplyMapper;
import com.nsu.evaluation.dao.StudentEvaluationsMapper;
import com.nsu.evaluation.dao.StudentMapper;
import com.nsu.evaluation.dao.StudentScoreMapper;
import com.nsu.evaluation.pojo.*;
import com.nsu.evaluation.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service("studentServiceImpl")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private StudentScoreMapper studentScoreMapper;

    @Autowired
    private StudentEvaluationsMapper studentEvaluationsMapper;

    @Autowired
    private FeedbackReplyMapper feedbackReplyMapper;

    /**
     * 学生用户登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public ServerResponse<Student> studentLogin(String username,String password){
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andIdCardNameEqualTo(username);
        long studentUserCount = studentMapper.countByExample(studentExample);
        if(studentUserCount == 0){
            return ServerResponse.createByErrorMessage("用户名不存在");
        }
        //设置密码。
        criteria.andUserPasswordEqualTo(password);
        List<Student> studentList = studentMapper.selectByExample(studentExample);
        if(null != studentList && studentList.size()>0){
            return ServerResponse.createBySuccess("登陆成功",studentList.get(0));
        }
        return ServerResponse.createByErrorMessage("登陆失败");
    }

    /**
     * 更新学生信息
     * @param student
     * @return
     */
    @Override
    public ServerResponse<Student> updateStudentInfoByPrimaryKey(Student student){
        int rowCount = studentMapper.updateByPrimaryKey(student);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("修改学生信息成功",student);
        }
        return ServerResponse.createByErrorMessage("修改学生信息失败");
    }

    /**
     * 获取学生成绩
     * @param studentScore
     * @return
     */
    @Override
    public ServerResponse<List<StudentScore>> getStudentScoreByExample(StudentScore studentScore){
        StudentScoreExample studentScoreExample = new StudentScoreExample();
        StudentScoreExample.Criteria criteria = studentScoreExample.createCriteria();
        //如果studentId不为空
        if(null != studentScore.getStudentId() && !studentScore.getStudentId().equals("")){
            criteria.andStudentIdEqualTo(studentScore.getStudentId());
        }
        //如果学年学期不为空
        if(null != studentScore.getAcademicSemesterId() && !studentScore.getAcademicSemesterId().equals("")){
            criteria.andAcademicSemesterIdEqualTo(studentScore.getAcademicSemesterId());
        }
        //如果教学安排不为空
        if(null != studentScore.getScheduleId() && !studentScore.getScheduleId().equals("")){
            criteria.andScheduleIdEqualTo(studentScore.getScheduleId());
        }
        List<StudentScore> studentScoreList = studentScoreMapper.selectByExample(studentScoreExample);
        return ServerResponse.createBySuccess("获取成绩列表成功",studentScoreList);
    }

    /**
     * 获取学生评价
     * @param studentId
     * @return
     */
    @Override
    public ServerResponse<StudentEvaluations> getStudentEvaluation(String studentId){
        //根据学生ID查找学生评价
        StudentEvaluationsExample studentEvaluationsExample = new StudentEvaluationsExample();
        StudentEvaluationsExample.Criteria criteria = studentEvaluationsExample.createCriteria();
        criteria.andStudentIdEqualTo(studentId);

        List<StudentEvaluations> studentEvaluationsList =
                studentEvaluationsMapper.selectByExample(studentEvaluationsExample);
        if(studentEvaluationsList != null && studentEvaluationsList.size() > 0){
            StudentEvaluations studentEvaluations = studentEvaluationsList.get(0);
            return ServerResponse.createBySuccess("获取学生评价成功",studentEvaluations);
        }
        return ServerResponse.createByErrorMessage("暂无学生评价");
    }

    /**
     * 提交评价申诉
     * @param feedbackReply
     * @return
     */
    @Override
    public ServerResponse<FeedbackReply> submitFeedbackReply(FeedbackReply feedbackReply){
        int rowCount = feedbackReplyMapper.insert(feedbackReply);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("提交申诉成功",feedbackReply);
        }
        return ServerResponse.createByErrorMessage("提交申诉失败");
    }

    /**
     * 根据id删除学生
     * @param studentId
     * @return
     */
    @Override
    public ServerResponse deleteStudentById(String studentId){
        int rowCount = studentMapper.deleteByPrimaryKey(studentId);
        if(rowCount>0){
            return ServerResponse.createBySuccess("删除学生信息成功");
        }
        return ServerResponse.createBySuccess("删除学生信息失败");
    }

    /**
     * 添加学生到班级
     * @param student
     * @param clazzId
     * @return
     */
    @Override
    public ServerResponse addStudentToClass(Student student,String clazzId){
        student.setClassId(clazzId);
        int rowCount = studentMapper.updateByPrimaryKey(student);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("添加学生到班级成功",student);
        }
        return ServerResponse.createByErrorMessage("添加学生到班级失败");
    }

    /**
     * 从班级中删除学生
     * @param student
     * @return
     */
    @Override
    public ServerResponse deleteStudentFromClass(Student student){
        student.setClassId("");
        int rowCount = studentMapper.updateByPrimaryKey(student);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("从班级中删除学生成功");
        }
        return ServerResponse.createByErrorMessage("从班级中删除学生失败");
    }

    /**
     * 获取未分班的学生列表
     * @return
     */
    @Override
    public ServerResponse<List<Student>> getNoDivisionClassStudent(){
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andClassIdEqualTo("");
        List<Student> studentList = studentMapper.selectByExample(studentExample);
        return ServerResponse.createBySuccess("获取未分班的学生列表成功",studentList);
    }

    /**
     * 修改学生班级
     * @param student
     * @return
     */
    @Override
    public ServerResponse<Student> updateStudentClassInfo(Student student,String classId){
        student.setClassId(classId);
        int rowCount = studentMapper.updateByPrimaryKey(student);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("修改学生班级成功",student);
        }
        return ServerResponse.createByErrorMessage("修改学生班级失败");
    }
}
